//
//  EBMBondSerachHistoryLocalStorage.m
//  eBondMall
//
//  Created by 赵世杰 on 2020/3/12.
//  Copyright © 2020 zhaoshijie. All rights reserved.
//

#import "EBMBondSerachHistoryLocalStorage.h"
#import "SJFileDataGainStorageTool.h"
#import "SJPathGainTool.h"


#define Option_BondSearch_History @"Option_BondSearch_History"



@implementation EBMBondSerachHistoryLocalStorage



/**
 获取搜索历史的本地数据
 
 @return 搜索历史
 */
+ (id)gainSharesSerachHistoryLocalStorage
{
    
    //  文件
    NSString *pathName = [NSString stringWithFormat:@"%@/%@",[SJPathGainTool gainDocumentsPath],Option_BondSearch_History];
    
    
    NSMutableArray *result_array = [SJFileDataGainStorageTool fileDataGainWithFilePath:pathName fileDataFormatType:SJFileDataGainStorageTool_FileDataFormatType_System fileDataGainType:SJFileDataGainStorageTool_FileDataJsonType_ArrayJson];
    
    
    if(!result_array)
    {
        result_array = [[NSMutableArray alloc]init];
    }
    
    NSMutableArray *array_m = [[NSMutableArray alloc]init];
    
    for (NSDictionary *dic in result_array)
    {
        EBMBondListModel *model = [[EBMBondListModel alloc]initWithContentDic:dic];

        
        [array_m addObject:model];
    }
    
    
    return array_m;
}


/**
 存储搜索历史的本地数据
 
 @param model 债券
 @param successBlock 成功
 @param failBlock 失败
 */
+ (void)storeBondSerachHistoryLocalStorageWithShareModel:(EBMBondListModel *)model
                                              successBlock:(void(^)(void))successBlock
                                                 failBlock:(void(^)(void))failBlock
{
    
    //  文件
    NSString *pathName = [NSString stringWithFormat:@"%@/%@",[SJPathGainTool gainDocumentsPath],Option_BondSearch_History];
    
    //  获取数据
    NSMutableArray *array_m = [SJFileDataGainStorageTool fileDataGainWithFilePath:pathName fileDataFormatType:SJFileDataGainStorageTool_FileDataFormatType_System fileDataGainType:SJFileDataGainStorageTool_FileDataJsonType_ArrayJson];

    
    //  是否存在
    BOOL isExistence = NO;
    
    if(!array_m)
    {
        array_m = [[NSMutableArray alloc]init];
        
    }
    else
    {
        for (NSInteger i=0; i<array_m.count; i++)
        {
            NSDictionary *dic = array_m[i];
            
            NSString *iId = dic[@"id"];
            
            if([iId isEqualToString:model.iId])
            {
                //  存在  移动位置
                
                if(i>0)
                {
                    [array_m exchangeObjectAtIndex:0 withObjectAtIndex:i];
                }
                
                isExistence = YES;
                
                break;
                
            }
        }
    }
    
    
    
    if(isExistence)
    {
        //  存在移动
    }
    else
    {
        //  不存在
        if(array_m.count>=8)
        {
            [array_m removeObjectAtIndex:0];
        }
        
        
        NSMutableDictionary *dic_c = [[NSMutableDictionary alloc]init];
        [dic_c setObject:model.iId forKey:@"id"];
        [dic_c setObject:model.bondAbbr forKey:@"bondAbbr"];
        
        //  添加股票代码
        [array_m insertObject:dic_c atIndex:0];
    }
    
    
    //  存储股票数据
    [SJFileDataGainStorageTool fileDataStorageWithJson:array_m filePath:pathName fileDataFormatType:SJFileDataGainStorageTool_FileDataFormatType_System successBlcok:^{
//        NSLog(@"成功");
        if(successBlock)
        {
            successBlock();
        }
    } failBlock:^{
//        NSLog(@"失败");
        if(failBlock)
        {
            failBlock();
        }
    }];
}






/**
 删除 历史搜索数据

 @param successBlock 成功
 @param failBlock 失败
 */
+ (void)removeBondSerachHistoryLocalStorageWithSuccessBlock:(void(^)(void))successBlock
                                                    failBlock:(void(^)(void))failBlock
{
    
    //  文件
    NSString *pathName = [NSString stringWithFormat:@"%@/%@",[SJPathGainTool gainDocumentsPath],Option_BondSearch_History];
    
    //  移除数据
    [SJFileDataGainStorageTool fileDataRemoveWithFilePath:pathName successBlcok:^{
        
        if(successBlock)
        {
            successBlock();
        }
        
    } failBlock:^{
        
        if(failBlock)
        {
            failBlock();
        }
        
    }];
    
}




@end
